/**
 * Created by zyy on 15/5/5.
 * zhangyuyu@superjia.com
 */
(function ($) {
    var link = {

        __day7: 7 * 24 * 3600 * 1000,

        init: function (container, options) {
            var self = this
            self.__container = container
            self.__options = options

            container.html(tpl)

            self.__panel = container.find('.mod-link')

            self.__initLink()

            self.__bindEvent()
        },

        __initLink: function () {
            var self = this
            var options = self.__options

            var fileModel = self.__fileModel = options.fileItems[0]
            var file = fileModel.toJSON()

            if (file.linkid) {
                self.__linkInfo(file.linkid)
            } else {
                $.ajax({
                    url: '/link/add/',
                    method: 'post',
                    dataType: 'json',
                    data: {
                        name: file.filename,
                        path: file.path,
                        all: 1,
                        auth: 0,
                        deadline: new Date().getTime() + self.__day7
                    },
                    success: function (data) {
                        self.__linkInfo(data.data.link)
                    }
                })
            }
        },

        __linkInfo: function (linkId) {
            var self = this
            var container = self.__container
            var options = self.__options

            $.ajax({
                url: '/link/info/',
                method: 'post',
                dataType: 'json',
                data: {
                    linkid: linkId
                },
                success: function (data) {
                    var linkData = self.__linkData = data.data
                    self.__wrapLinkData(self.__linkData)
                    container.find('.link-wrap').html(Mustache.render(linkTpl, linkData))
                }
            })
        },

        __wrapLinkData: function (linkData) {
            linkData.isAuth = linkData.auth == '1'
            linkData.isDeadline = !!linkData.deadline
            linkData.isPassword = !!linkData.password
        },

        __bindEvent: function () {
            var self = this
            var panel = self.__panel

            panel.on('click', '.back', function () {
                $.jps.publish('action-back')
            })

            panel.on('click', '.opt-btn', function () {
                var linkData = self.__linkData
                cordova.exec(function(){
                        App.common.modules.smallnote('恭喜您，复制成功！')
                    }, $.noop,
                    'Clipboard', 'copy',
                    [{
                        content: linkData.shorturl
                    }]
                );
            })

            panel.on('click', '.checkbox', function () {
                var linkData = self.__linkData
                var item = $(this)
                var info = item.data('info')
                item.toggleClass('disabled')

                if (info == 'cancel') {
                    if (!self.__cancelLink()) {
                        item.toggleClass('disabled')
                    }
                    return false
                }

                var postData = $.extend({}, linkData)

                var isDisabled = item.hasClass('disabled')
                switch (info) {
                    case 'auth':
                        postData.auth = isDisabled ? 0 : 1
                        break
                    case 'deadline':
                        postData.deadline = isDisabled ? '' : new Date().getTime() + self.__day7
                        break
                    case 'password':
                        if (isDisabled) {
                            postData.password = ''
                            item.parent().find('.password-input').addClass('hide')
                        } else {
                            self.__setPassword()
                            return false
                        }
                        break
                }

                postData.linkid = linkData.linkid
                $.ajax({
                    url: '/link/update/',
                    method: 'post',
                    dataType: 'json',
                    data: postData,
                    success: function (data) {
                        if(data.code == 0) {
                            self.__linkData = postData
                        }
                    }
                })
            })

            panel.on('keyup', '.password-input', function () {
                var pwd = $.trim($(this).val())

                if (!pwd) return false

                var postData = $.extend({}, self.__linkData, {
                    password: pwd
                })
                $.ajax({
                    url: '/link/update/',
                    method: 'post',
                    dataType: 'json',
                    data: postData,
                    success: function (data) {
                        if(data.code == 0) {
                            self.__linkData = postData
                        }
                    }
                })
            })
        },

        __cancelLink: function () {
            var self = this
            var linkData = self.__linkData

            if (confirm('您确定要删除此链接吗？')) {
                $.ajax({
                    url: '/link/cancel/',
                    method: 'post',
                    dataType: 'json',
                    data: {
                        linkid: linkData.linkid
                    },
                    success: function (data) {
                        self.__fileModel.set('linkid', '')
                        $.jps.publish('action-back')
                    }
                })
                return true
            }
            return false
        },

        __setPassword: function () {
            var self = this
            var panel = self.__panel

            var linkData = self.__linkData

            panel.find('.password-input').removeClass('hide').select().val('')
        }
    }

    var tpl = '<div class="mod-link">' +
        '   <div class="action-header">' +
        '       <a class="back">&lt;</a><div class="title">分享</div><div class="opt-btn">复制链接</div>' +
        '   </div>' +
        '   <div class="link-wrap">' +
        '   </div>' +
        '</div>'

    var linkTpl = '' +
        '<div class="link-info">' +
        '   <div class="title">' +
        '       <p class="intro">您的共享链接已成功创建</p>' +
        '       <p class="shorturl">{{shorturl}}</p>' +
        '   </div>' +
        '   <div class="info-item">' +
        '       <div class="clr checkbox-item">' +
        '           <div class="desc f-l">可下载：</div>' +
        '           <div class="checkbox f-r {{^isAuth}}disabled{{/isAuth}}" data-info="auth">' +
        '               <span class="sliding"></span>' +
        '           </div>' +
        '       </div>' +
        '       <p class="intro">允许用户从此链接下载文件</p>' +
        '   </div>' +
        '   <div class="info-item">' +
        '       <div class="clr checkbox-item">' +
        '           <div class="desc f-l">七天后到期：</div>' +
        '           <div class="checkbox f-r {{^isDeadline}}disabled{{/isDeadline}}" data-info="deadline">' +
        '               <span class="sliding"></span>' +
        '           </div>' +
        '       </div>' +
        '       <p class="intro">在七天后自动禁用此链接</p>' +
        '   </div>' +
        '   <div class="info-item">' +
        '       <div class="clr checkbox-item">' +
        '           <div class="desc f-l">密码访问：</div>' +
        '           <div class="f-l"><input class="password-input {{^isPassword}}hide{{/isPassword}}" type="text" name="password" value="{{password}}"/></div>' +
        '           <div class="checkbox f-r {{^isPassword}}disabled{{/isPassword}}" data-info="password">' +
        '               <span class="sliding"></span>' +
        '           </div>' +
        '       </div>' +
        '       <p class="intro">需要密码以访问此链接</p>' +
        '   </div>' +
        '   <div class="info-item">' +
        '       <div class="clr checkbox-item">' +
        '           <div class="desc f-l">禁用链接：</div>' +
        '           <div class="checkbox f-r disabled" data-info="cancel">' +
        '               <span class="sliding"></span>' +
        '           </div>' +
        '       </div>' +
        '       <p class="intro">永远删除当前链接</p>' +
        '   </div>' +
        '</div>'

    App.modules.linkAction = link
})(jQuery)